﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElsProp.Model
{
    public class MyHelp
    {
        /// <summary>
        /// 属性计算
        /// </summary>
        /// <param name="props"></param>
        /// <exception cref="NotImplementedException"></exception>
        public static string SubPropModel(List<PropModel> props, System.Collections.ObjectModel.ObservableCollection<SelectSuit> selectSuitItem)
        {
            //装备属性
            List<ItemProperties> itemProperties = new List<ItemProperties>();
            //套装属性
            List<ItemProperties> suitProperties = new List<ItemProperties>();
            List<SelectSuit> hasSuit = new List<SelectSuit>();
            foreach (var propModel in props)
            {
                itemProperties.AddRange(propModel.Properties);
                if (hasSuit.Find(x=>x.suit==propModel.Suit&&x.suitItem==propModel.SuitName)==null)
                {
                    hasSuit.Add(new SelectSuit()
                    {
                        suitItem =
                        propModel.SuitName,
                        suit = propModel.Suit
                    });
                }
            }
            foreach (var suit in hasSuit)
            {
                if (selectSuitItem!=null&& selectSuitItem.Count>0)
                {
                    int has = 0;
                    foreach (var suitItem in selectSuitItem)
                    {
                        if (suitItem.select&&suitItem.suitItem== suit.suitItem&&suitItem.suit==suit.suit)
                        {
                            has++;
                        }
                    }
                    if (has==0)
                    {
                        continue;
                    }
                }
                var count = props.FindAll(x => x.SuitName == suit.suitItem&&x.Suit==suit.suit);
                if (count.Count > 0)
                {
                    foreach (var item in count)
                    {
                        if (item.SuitProperties != null && item.SuitProperties.Count > 0)
                        {
                            foreach (var propModel in item.SuitProperties)
                            {
                                if (propModel.Condition <= count.Count)
                                {
                                    suitProperties.Add(propModel);
                                }
                            }
                            break;
                        }
                    }
                }
            }
            string dstvalue = "";
            itemProperties.AddRange(suitProperties);
            var group = itemProperties.GroupBy(x => x.PropertiesName);
            foreach (var propModel in group)
            {
                dstvalue += propModel.Key;
                float value = 0;
                foreach (var item in propModel)
                {
                    if (item.PropertiesValue.Contains("%"))
                    {
                        item.PropertiesValue = item.PropertiesValue.Replace("%", "");
                    }
                    float.TryParse(item.PropertiesValue, out float va);
                    value += va;
                }
                if (dstvalue.Contains("{0}"))
                {
                    dstvalue = string.Format(dstvalue, value);
                }
                else
                {
                    dstvalue += value;
                }
                dstvalue += "\r\n";
            }
            return dstvalue;
        }

    }

}
